package com.imageim.yimirror.common.util;

import android.animation.ValueAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;


/**
 * Created by Bruce on 2017/8/11.
 */

public class AnimUtil {

    private static final String TAG = "AnimUtil";

    public static void moveUpWithFade(View view) {
        ObjectAnimator moveUp = ObjectAnimator.ofFloat(view, "translationY", UIUtils.dip2px(view.getContext(), 100), 0);
        ObjectAnimator fadeIn = ObjectAnimator.ofFloat(view, "alpha", 0.2f, 1);
        AnimatorSet animSet = new AnimatorSet();
        animSet.playTogether(moveUp, fadeIn);
        animSet.setDuration(1000);
        animSet.setInterpolator(new DecelerateInterpolator());
        animSet.start();
    }

    public static void scaleUp(final View view) {
        if (view != null) {
            view.setScaleX(0);
            view.setScaleY(0);
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0, 1f);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0, 1f);
            AnimatorSet animSet = new AnimatorSet();
            //            animSet.setTarget(view);
            animSet.setDuration(1100);
            //            animSet.setInterpolator(new OvershootInterpolator());
            animSet.playTogether(scaleX, scaleY);
            animSet.setStartDelay(120);
            animSet.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationStart(Animator animation) {
                    view.setVisibility(View.VISIBLE);
                }
            });
            animSet.start();
        }
    }

    public static void scaleToDismiss(final View view) {
        if (view != null) {
            view.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0f);
                    ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0f);
                    ObjectAnimator fadeout = ObjectAnimator.ofFloat(view, "alpha", 1, 0.1f);
                    AnimatorSet animSet = new AnimatorSet();
                    animSet.setTarget(view);
                    animSet.setDuration(650);
                    animSet.addListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            view.setVisibility(View.GONE);
                        }
                    });
                    animSet.setInterpolator(new AccelerateInterpolator());
                    animSet.playTogether(scaleX, scaleY, fadeout);
                    animSet.start();
                }
            }, 320);
        }
    }


    /**
     * 点击放大图片动画效果
     * 遍历ViewGroup
     */

    public static void resetScale(RecyclerView parent, int currIndex) {
        for (int i = 0; i < parent.getChildCount(); i++) {

            if (i == currIndex) {
                View view = parent.getChildAt(currIndex);
                AnimatorSet set = new AnimatorSet();
                set.playTogether(
                        ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.3f),
                        ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.3f)
                );
                set.setDuration(300).start();

            } else {
                View view = parent.getChildAt(i);
                AnimatorSet set = new AnimatorSet();
                set.playTogether(
                        ObjectAnimator.ofFloat(view, "scaleX", 1f),
                        ObjectAnimator.ofFloat(view, "scaleY", 1f)
                );
                set.setDuration(300).start();
            }

        }
    }


    /**
     * 显示手势提醒操作
     * 平移动画
     */
    public static void gestureTipAnim(View view) {
        android.animation.ObjectAnimator translationX = android.animation.ObjectAnimator.ofFloat(view, "translationX", 0, -400f, 0);
        translationX.setDuration(1500);
        translationX.start();
    }


    /**
     * 搭配图从上往下滑动显示出来
     *
     * @param height 最终显示出来的区域的高度dp
     */
    public static void slideShowArea(View v, int height, long durationTime) {
        v.setVisibility(View.VISIBLE);
        android.animation.ValueAnimator animator = createDropAnimator(v, 0,
                height);
        animator.setDuration(durationTime);
        animator.start();

    }


    /**
     * 动画过程中不断改变高度
     */
    private static android.animation.ValueAnimator createDropAnimator(final View v, int start, int end) {
        android.animation.ValueAnimator animator = android.animation.ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new android.animation.ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(android.animation.ValueAnimator arg0) {
                int value = (int) arg0.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
                layoutParams.height = value;
                v.setLayoutParams(layoutParams);

            }
        });

        return animator;
    }


    /**
     * 从下往上滑动隐藏布局
     */
    public static void slideHideArea(View view, View selectView, long durationTime) {
        int origHeight = view.getHeight();
        android.animation.ValueAnimator animator = createDropAnimator(view, origHeight, 0);
        animator.addListener(new android.animation.AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(android.animation.Animator animation) {
                view.setVisibility(View.GONE);
            }

        });
        animator.setDuration(durationTime);
        animator.start();

    }

    /**
     * 时尚趋势品牌界面属性动画
     */
    public static void fashionItemAnim(View view) {
        AnimatorSet set = new AnimatorSet();
        view.setPivotX(view.getWidth() / 2);
        view.setPivotY(view.getHeight());
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0, 1);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0, 1);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", 0, 1);
        set.playTogether(scaleX, scaleY, alpha);
        set.setInterpolator(new LinearInterpolator());
        set.setDuration(500);
        set.start();
    }


    public static ObjectAnimator rotateAnim(View view, int duration, boolean clockwise) {
        ObjectAnimator rotate;
        if (clockwise) {
            rotate = ObjectAnimator.ofFloat(view, "rotation", 0, 360);
        } else {
            rotate = ObjectAnimator.ofFloat(view, "rotation", 0, -360);
        }
        rotate.setDuration(duration);
        rotate.setRepeatCount(ValueAnimator.INFINITE);
        rotate.setRepeatMode(ValueAnimator.INFINITE);
        //旋转动画不停顿
        rotate.setInterpolator(new LinearInterpolator());
        rotate.start();
        return rotate;
        //        rotate.cancel();
    }

    public static void scaleBlowUp(View view) {
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.5f, 0.7f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.5f, 0.7f);
        set.playTogether(scaleX, scaleY);
        set.setInterpolator(new LinearInterpolator());
        set.setDuration(1000);
        set.start();
    }


    public static AnimatorSet scaleShowRedPacketTip(View view) {
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0.3f, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0.3f, 1f);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", 0.3f, 1f);
        set.playTogether(scaleX, scaleY, alpha);
        set.setDuration(400);
        set.setInterpolator(new LinearInterpolator());
        set.start();
        return set;
    }

}
